<?php

namespace app\admin\controller;

use think\Lang;

/**
    我要添砖zjw
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Addbricks extends AdminControl
{

    public function _initialize()
    {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/goodsclass.lang.php');
    }


    /**
     * 我要添砖首页
     */
    public function index()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        /**
         * 查询条件
         */
        $where = array();
        #设备名称
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['equipment_name'] = array('like', '%' . $search_goods_name . '%');
        }

        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $where['b_id'] = $b_id;
        }

        $addbricks_model = model('addbricks');
        $where['sort'] = 0;
        $where['bricks_del'] = 0;
        //判断是那种类型
        $bricks_list = $addbricks_model->getAddbricksList($where, '*', 10, 'create_time desc');
// var_dump($bricks_list);exit;
        $this->assign('bricks_list', $bricks_list);
        $this->assign('show_page', $addbricks_model->page_info->render());

        // $storage_array = $goods_model->calculateStorage($goods_list);
        // $this->assign('storage_array', $storage_array);

        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);
        $this->assign('search_goods_name', $search_goods_name);
        $this->assign('b_id', $b_id);

        $this->assign('enterprise_status', array('0' => '待审核', '1' => '审核通过', '2' => '审核失败'));

        $this->assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));

        $type = input('param.type');
        if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
            $type = 'allgoods';
        }

        $this->assign('type', $type);
        $this->setAdminCurItem('two_goods');
        return $this->fetch();
    }


    /**
     * 违规下架
     */
    public function two_goods_no()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        /**
         * 查询条件
         */
        $where = array();
        #设备名称
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['equipment_name'] = array('like', '%' . $search_goods_name . '%');
        }

        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $where['brand_id'] = $b_id;
        }

        $addbricks_model = model('addbricks');
        $where['sort'] = 0;
        $where['bricks_del'] = 0;
        $where['bricks_status'] = 2;
        //判断是那种类型
        $bricks_list = $addbricks_model->getAddbricksList($where, '*', 10, 'create_time desc');
// var_dump($bricks_list);exit;
        $this->assign('bricks_list', $bricks_list);
        $this->assign('show_page', $addbricks_model->page_info->render());

        $this->assign('search_goods_name', $search_goods_name);
        $this->assign('b_id', $b_id);

        // $storage_array = $goods_model->calculateStorage($goods_list);
        // $this->assign('storage_array', $storage_array);

        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);

        $this->assign('enterprise_status', array('0' => '待审核', '1' => '审核通过', '2' => '审核失败'));

        $this->assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));

        $type = input('param.type');
        if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
            $type = 'allgoods';
        }

        $this->assign('type', $type);
        $this->setAdminCurItem('two_goods_no');
        return $this->fetch('index');
    }

    /**
     * 待审核
     */
    public function two_goods_wait()
    {
                $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        /**
         * 查询条件
         */
        $where = array();
        #设备名称
        $search_goods_name = trim(input('param.search_goods_name'));
        if ($search_goods_name != '') {
            $where['equipment_name'] = array('like', '%' . $search_goods_name . '%');
        }

        $b_id = intval(input('param.b_id'));
        if ($b_id > 0) {
            $where['brand_id'] = $b_id;
        }

        $addbricks_model = model('addbricks');
        $where['sort'] = 0;
        $where['bricks_del'] = 0;
        $where['bricks_status'] = 0;
        //判断是那种类型
        $bricks_list = $addbricks_model->getAddbricksList($where, '*', 10, 'create_time desc');
// var_dump($bricks_list);exit;
        $this->assign('bricks_list', $bricks_list);
        $this->assign('show_page', $addbricks_model->page_info->render());

        $this->assign('search_goods_name', $search_goods_name);
        $this->assign('b_id', $b_id);

        // $storage_array = $goods_model->calculateStorage($goods_list);
        // $this->assign('storage_array', $storage_array);

        // 品牌
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('search', $where);
        $this->assign('brand_list', $brand_list);

        $this->assign('enterprise_status', array('0' => '待审核', '1' => '审核通过', '2' => '审核失败'));

        $this->assign('ownShopIds', array_fill_keys(model('store')->getOwnShopIds(), true));

        $type = input('param.type');
        if (!in_array($type, array('lockup', 'waitverify', 'allgoods'))) {
            $type = 'allgoods';
        }

        $this->assign('type', $type);
        $this->setAdminCurItem('two_goods_wait');
        return $this->fetch('index');
    }


    /**
     * 我要添砖
     */
    public function add_bricks()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);
        $goodsclass_model = model('goodsclass');
        $brand_list = model('brand')->getBrandPassedList(array());
        $this->assign('brand_list', $brand_list);
        $area_model = model('area');
        if (!request()->isPost()) {
            $type = [
                'class_id' => 0,
            ];
            $this->assign('type', $type);
            #获取顶级分类
            $where = array();
            $where['gc_parent_id'] = 0;
            $where['gc_show'] = 1;
            $field = '';
            $field = 'gc_id,gc_name';
            $top_goods_class = $goodsclass_model->getgoodsclassinfo($where, $field);
            if (empty($top_goods_class)) {
                $this->error('暂无顶级分类');
            }

            #获取品牌
            $field = '';
            //父类列表，只取到第二级
            $parent_list = $goodsclass_model->getTreeClassList(2);
            $gc_list = array();
            if (is_array($parent_list)) {
                foreach ($parent_list as $k => $v) {
                    $parent_list[$k]['gc_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['gc_name'];
                    if ($v['deep'] == 1)
                        $gc_list[$k] = $v;
                }
            }
            $this->assign('gc_list', $gc_list);
            $this->assign('top_goods_class', $top_goods_class);

            $brand_array = [
                'xianshi_state' => 1,
            ];
            $this->assign('brand_array', $brand_array);
            $this->setAdminCurItem('add_bricks');
            return $this->fetch('add_bricks');
        } else {
            $data = input('post.');

            $insert_array = array();
            $insert_array['equipment_name'] = $data['equipment_name'];

            //处理分类
            $class = db('goodsclass')->where(['gc_id'=>$data['class_id']])->find();
            if(empty($class['gc_parent_id'])){
                //没父值的话就是一级分类id
                $insert_array['gc_one_id'] = $class['gc_id'];
                $insert_array['gc_id'] = $class['gc_id'];
                $insert_array['gc_name'] = $class['gc_name'];
            }else{
                //有父值的话再往上找
                $class_one = db('goodsclass')->where(['gc_id'=>$class['gc_parent_id']])->find();
                if(empty($class_one['gc_parent_id'])){ 
                    $insert_array['gc_one_id'] = $class_one['gc_id'];
                    $insert_array['gc_two_id'] = $class['gc_id'];
                    $insert_array['gc_id'] = $class['gc_id'];
                    $insert_array['gc_name'] = $class_one['gc_name'] .'>'. $class['gc_name'] ;
                }else{
                    $class_two = db('goodsclass')->where(['gc_id'=>$class_one['gc_parent_id']])->find();
                    $insert_array['gc_one_id'] =  $class_two['gc_id'];
                    $insert_array['gc_two_id'] =  $class_one['gc_id'];
                    $insert_array['gc_three_id'] = $class['gc_id'];
                    $insert_array['gc_id'] = $class['gc_id'];
                    $insert_array['gc_name'] = $class_two['gc_name'].'>'.$class_one['gc_name'] .'>'. $class['gc_name'] ;
                }
            }

            //处理品牌
            if (trim(input('post.b_id')) == 0) {

            } else {
                $brand_id = intval(input('post.b_id'));
                $brand_name = input('post.b_name');
            }

            //处理图片
            $files = input('pic/a');
            var_dump($files);exit;
             
            //基本信息
            $insert_array['b_id'] = $brand_id ? $brand_id : '';
            $insert_array['b_name'] = $brand_name ? $brand_name : '';
            $insert_array['bricks_spec'] = $data['bricks_spec'];
            $insert_array['bricks_type'] = $data['bricks_type'];
            $insert_array['bricks_images'] = $files['-11'];
            $insert_array['bricks_status'] = 1;
            $insert_array['create_time'] = time();
            $insert_array['status'] = input('param.status');
            $insert_array['is_store'] = 0;
            $new_id = db('addbricks')->insertGetId($insert_array);
            if ($new_id) {
                return json(['url' => url('Addbricks/index'), 'code' => 1, 'msg' => '添加成功']);
            } else {
                return json(['url' => url('Addbricks/index'), 'code' => 0, 'msg' => '添加失败']);
            }
        }
    }

        /*
     * 上下架
     */
    public function equipment_status()
    {
        $bricks_id = trim(input('param.bricks_id'));
        $status = trim(input('param.status'));
        if (empty($bricks_id)) {
            ds_json_encode(10001, '参数错误');
        }
        $data = array();
        if($status == 1){
            $data['status'] = 2;
        }else{
            $data['status'] = 1;
        }
        $rs = db('addbricks')->where('bricks_id', $bricks_id)->data($data)->update();
        if ($rs) {
            ds_json_encode(10000, '操作成功');
        } else {
            ds_json_encode(10001, '操作失败');
        }
    }

    /**
     * 删除
     */
    public function bricks_del() {
        $bricks_id = input('param.bricks_id');
        db('addbricks')->where(['bricks_id'=>$bricks_id])->update(['bricks_del'=>1]);
        ds_json_encode(10000, lang('ds_common_del_succ'));
    }


    #审核页
    public function goods_verify()
    {
        $bricks_id = input('param.bricks_id');
        $bricks_info = db('addbricks')->where(['bricks_id'=>$bricks_id])->find();
        if (!request()->isPost()) {
            $this->assign('joinin_detail', $bricks_info);
            $this->assign('bricks_id', $bricks_id);
            $config = db('config')->where('code', 'qiniu_http')->find();
            $this->assign('http_name', $config['value']);
            $this->assign('is_verify', true);
            return $this->fetch('goods_verify');
        } else {
            // p($_POST);die;
            if (input('verify_state') == 1) {
                #审核通过
                // $data['commonid'] = input('commonid');
                $data['goods_status'] = 1;
                $data['two_insertprice'] = input('price');

            } else {
                #审核失败
                //$data['commonid'] = input('commonid');
                $data['goods_status'] = 2;
                $data['verify_reason'] = input('verify_reason');
            }

            $return = db('goodscommon')->where('goods_commonid', $id)->update($data);
            if ($return) {
                // 添加操作日志
                $this->log('商品审核commonid' . $id, 1);
                dsLayerOpenSuccess(lang('ds_common_op_succ'));

            } else {
                $this->log('商品审核commonid' . $id, 0);
                dsLayerOpenSuccess('审核失败');

            }
        }
    }


    /**
     * 审核
     */
    public function bricks_verify() {
        $bricks_id = input('param.bricks_id');
        $status = input('post.verify_type');
        $reason = input('post.joinin_message');

        $enterprise_info = db('addbricks')->where(['bricks_id'=>$bricks_id])->find();
        if($status == 'pass'){
            db('addbricks')->where(['bricks_id'=>$bricks_id])->update(['bricks_status'=>1,'bricks_reason'=>$reason]);
        }else{
            db('addbricks')->where(['bricks_id'=>$bricks_id])->update(['bricks_status'=>2,'bricks_reason'=>$reason]);
        }
        dsLayerOpenSuccess('审核成功');
    }


   //详情
    public function goods_content()
    {
        $bricks_id = input('param.bricks_id');
        $bricks_info = db('addbricks')->where(['bricks_id'=>$bricks_id])->find();
         $this->assign('joinin_detail', $bricks_info);

        $member_name = db('member')->where(['member_id'=>$bricks_info['member_id']])->find();
        $this->assign('member_name', $member_name);
        
        $this->assign('bricks_id', $bricks_id);
        $config = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $config['value']);
        $this->assign('is_verify', false);
        return $this->fetch('goods_verify');
    }

    /**
     * 编辑
     */
    public function edit_goods()
    {
        $file_http = db('config')->where('code', 'qiniu_http')->find();
        $this->assign('http_name', $file_http['value']);

        $id = input('param.commonid');
        if (empty($id)) {
            $this->error('请选择要操作的商品');
        }
        $goods_info = db('goodscommon')->where('goods_commonid', $id)->find();
        if (empty($goods_info) || $goods_info['goods_lock'] == 1) {
            $this->error('操作错误');
        }
        /* if ($goods_info['goods_status'] == 1) {
             $this->error('请将商品下架后处理');
         }*/

        $goodsclass_model = model('goodsclass');
        $area_model = model('area');
        $brand_model = model('brand');
        if (!request()->isPost()) {
            $type = [
                'class_id' => 0,
            ];
            $this->assign('type', $type);
            $goods_list = db('goods')->where('goods_commonid', $id)->find();
            $goods_info['goods_storage'] = $goods_list['goods_storage'];
            $this->assign('goods_info', $goods_info);
            $goods = db('goods')->where('goods_commonid', $id)->find();
            $goods_images = db('goodsimages')->where('goods_commonid', $id)->select();
            $this->assign('goods_images', $goods_images);
            $area_info = explode(',', $goods_info['two_goods_address']);
            $this->assign('area_info', $area_info);
            #获取手机顶级分类
            $where = array();
            $where['gc_parent_id'] = 0;
            $where['gc_show'] = 1;
            $field = '';
            $field = 'gc_id,gc_name';
            $top_goods_class = $goodsclass_model->getgoodsclassinfo($where, $field);
            if (empty($top_goods_class)) {
                $this->error('暂无顶级分类');
            }

            #获取省份
            $where = array();
            $where['area_parent_id'] = 0;
            $top_area_class = $area_model->getarea_list($where, 'area_id,area_name');
            if (empty($top_area_class)) {
                $this->error('暂无省份');
            }
            $this->assign('top_area_class', $top_area_class);
            #获取品牌
            $field = '';
            //父类列表，只取到第二级
            $parent_list = $goodsclass_model->getTreeClassList(2);
            $gc_list = array();
            if (is_array($parent_list)) {
                foreach ($parent_list as $k => $v) {
                    $parent_list[$k]['gc_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['gc_name'];
                    if ($v['deep'] == 1)
                        $gc_list[$k] = $v;
                }
            }
            $this->assign('gc_list', $gc_list);

            $brand_list = $brand_model->getbrand_list($field);

            if (empty($brand_list)) {
                $this->error('暂无品牌');
            }


            $this->assign('brand_list', $brand_list);


            $this->assign('top_goods_class', $top_goods_class);
            $this->setAdminCurItem('edit_goods');
            return $this->fetch('edit_goods');
        } else {
            $insert_array = array();
            $goods_name = trim(input('post.goods_name'));
            $insert_common_array = array();
            $insert_common_array['goods_name'] = $goods_name;

            if (trim(input('post.class_id'))&&input('post.class_id')!='1000000') {
                $class_id = intval(input('post.class_id'));
                $class_name = input('post.class_name');
                $class_info = db('goodsclass')->where(['gc_id' => $class_id])->find();
                if ($class_info['gc_parent_id'] == 0) {
                    #顶级
                    $insert_common_array['gc_id_1'] = $class_id;
                    $insert_common_array['gc_name'] = $class_name;
                } else {
                    #查询判断父级信息
                    $pranet_info = db('goodsclass')->where('gc_id', $class_info['gc_parent_id'])->find();
                    if ($pranet_info['gc_parent_id'] == 0) {
                        #第二级
                        $insert_common_array['gc_id_1'] = $class_info['gc_parent_id'];
                        $insert_common_array['gc_id_2'] = $class_id;
                        $insert_common_array['gc_name'] = $pranet_info['gc_name'] . ',' . $class_name;
                    } else {
                        $pranets_info = db('goodsclass')->where('gc_id', $pranet_info['gc_parent_id'])->find();
                        $insert_common_array['gc_id'] = $class_id;
                        $insert_common_array['gc_id_1'] = $pranets_info['gc_id'];
                        $insert_common_array['gc_id_2'] = $pranet_info['gc_id'];
                        $insert_common_array['gc_name'] = $pranets_info['gc_name'] . ',' . $pranet_info['gc_name'] . ',' . $class_name;

                    }

                }
            } else if(input('post.class_id')=='1000000') {

            }else{
                $insert_common_array['gc_id'] = '';
                $insert_common_array['gc_id_1'] = '';
                $insert_common_array['gc_id_2'] = '';
                $insert_common_array['gc_name'] = '';
            }

            if (trim(input('post.b_id')) == 0) {
                $brand_id = '0';

                $brand_name = '';
            } else {
                $brand_id = intval(input('post.b_id'));
                $brand_name = input('post.b_name');
            }

            $count = intval(input('post.count'));


            $phone = input('post.phone');


            $price = intval(input('post.price'));

            if(input('post.district_id')!=1){
                $area_list = input('post.company_address');
                $insert_common_array['two_goods_address'] = $area_list;
            }
            $content = input('post.content');

            $pic = input('param.pic/a');
            if ($pic['0'] != $goods_info['goods_image']) {
                $insert_common_array['goods_image'] = $pic['0'];
            }

            $insert_common_array['brand_id'] = $brand_id ? $brand_id : '';
            $insert_common_array['brand_name'] = $brand_name ? $brand_name : '';
            $insert_common_array['goods_price'] = $price;
            $insert_common_array['goods_body'] = $content;
            $insert_common_array['two_phone'] = $phone;
            $res = db('goodscommon')->where('goods_commonid', $id)->data($insert_common_array)->update();

            $new_info = db('goodscommon')->where('goods_commonid', $id)->find();
            $insert_goods_array = array();
            $insert_goods_array['goods_name'] = $goods_name;
            $insert_goods_array['gc_id'] = $new_info['gc_id'];
            $insert_goods_array['gc_id_1'] = $new_info['gc_id_1'];
            $insert_goods_array['gc_id_2'] = $new_info['gc_id_2'];
            $insert_goods_array['brand_id'] = $new_info['brand_id'];
            $insert_goods_array['goods_image'] = $new_info['goods_image'];
            $insert_goods_array['goods_price'] = $price;
            $insert_goods_array['goods_promotion_price'] = $price;
            $insert_goods_array['goods_storage'] = $count;
            $newgoods_id = db('goods')->where('goods_commonid', $id)->data($insert_goods_array)->update();
            //p($newgoods_id);die;
            db('goodsimages')->where('goods_commonid', $id)->delete();
            foreach ($pic as $k => $v) {
                $data = array();

                $data = ['goods_commonid' => $id, 'store_id' => 0, 'color_id' => 0, 'goodsimage_url' => $v];
                if ($k == 0) {
                    $data['goodsimage_isdefault'] = 1;
                } else {
                    $data['goodsimage_isdefault'] = 0;
                }

                db('goodsimages')->data($data)->insert();
            }
            return json(['url' => url('Twogoods/index'), 'code' => 1, 'msg' => '修改成功']);
        }
    }

    


        /**
     * AJAX获取商品分类
     */
    public function ajax_class()
    {
        $id = intval(input('param.id'));
        $deep = intval(input('param.deep'));
        if ($id <= 0 || $deep <= 0 || $deep >= 4) {
            echo 'false';
            exit();
        }
        $deep += 1;
        $goodsclass_model = model('goodsclass');

        // 验证分类是否存在
        $gc_info = $goodsclass_model->getGoodsclassInfoByIds($id);
        if (empty($gc_info)) {
            echo 'false';
            exit();
        }

        // 读取商品分类
        if ($deep != 4) {
            $gc_list = $goodsclass_model->getGoodsclass($this->_store_id, $id, $deep);
        }
        // 分类不为空输出分类信息
        if (!empty($gc_list)) {
            $data = array('type' => 'class', 'data' => $gc_list, 'deep' => $deep);
        } else {
            // 查询类型
            $type_model = model('type');
            $spec_list = $type_model->getSpecByType(array('type_id' => $gc_info['type_id']), 't.type_id, s.*');

            $data = array('type' => 'spec', 'data' => $spec_list, 'gcid' => $id, 'aa' => $gc_info, 'gcid' => $id, 'deep' => $deep);
        }

        echo json_encode($data);
        exit();
    }



    /**
     * AJAx 获取分类
     */
    public function ajax_get_goods_class()
    {
        if (request()->isAjax()) {
            $goodsclass_model = model('goodsclass');
            $gc_id = trim(input('gc_id'));
            #获取手机顶级分类
            $where = array();
            $where['gc_parent_id'] = $gc_id;
            $where['gc_show'] = 1;
            $field = '';
            $field = 'gc_id,gc_name';
            $goods_class = $goodsclass_model->getgoodsclassinfo($where, $field);
            return $goods_class;
        } else {
            $this->error('访问错误');
        }


    }

    /**
     * AJAx 获取地区
     */
    public function ajax_get_area_class()
    {
        if (request()->isAjax()) {
            $area_model = model('area');
            $area_id = trim(input('area_id'));
            #获取手机顶级分类
            $where = array();
            $where['area_parent_id'] = $area_id;
            $field = '';
            $field = 'area_id,area_name';
            $area_class = $area_model->getarea_list($where, $field);
            return $area_class;
        } else {
            $this->error('访问错误');
        }


    }

    /*
      ajax 上传图片

     */
    public function ajax_upload()
    {
        $file_type = array('0' => 'image/jpeg', '1' => 'image/jpg', '2' => 'image/png');
        if (in_array($_FILES['file']['type'], $file_type)) {
            $file_name = input('post.name');
            $upload_info = upload_qiniu('file');
            return $upload_info;
        } else {
            return $data['msg'] = 0;

        }
    }

/**
     * @description:   @MrLiu
     * @return array
     */
    protected function getAdminItemList()
    {
        $menu_array = array(
             array(
                'name' => 'two_goods',
                'text' => '管理',
                'url' => url('Addbricks/index')
            ),
            array(
                'name' => 'two_goods_no',
                'text' => '添砖记录',
                'url' => url('Addbricks/two_goods_no')
            ),
            array(
                'name' => 'two_goods_wait',
                'text' => '待审核',
                'url' => url('Addbricks/two_goods_wait')
            ),
            array(
                'name' => 'add_bricks',
                'text' => '添加设备',
                'url' => url('Addbricks/add_bricks')
            ),
        );
        if (request()->action() === 'edit_goods') {
            $menu_array[] = array(
                'name' => 'edit_goods',
                'text' => '修改',
                'url' => url('Addbricks/edit_goods')
            );
        }
        return $menu_array;
    }
}

?>
